package com.gitee.xqlr.graphql.client.request;

import com.gitee.xqlr.graphql.client.request.param.RequestPara;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 杜文康
 * @version 1.0
 * @date 2019/8/12 15:18
 */
@SuppressWarnings("UnusedReturnValue")
public abstract class GraphqlRequest {

    private final Map<String, String> headers = new HashMap<String, String>();

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers.putAll(headers);
    }

    private String requestName;

    private RequestPara requestPara;

    private String ret;

    protected GraphqlRequest(String requestName) {
        this.requestName = requestName;
    }

    public RequestPara addPara(String key, Object val) {
        return getRequestPara().addPara(key, val);
    }

    public RequestPara addObjPara(String key, Object val) {
        return getRequestPara().addPara(key, val);
    }

    public RequestPara getRequestPara() {
        if (this.requestPara == null) {
            this.requestPara = RequestPara.build();
        }
        return this.requestPara;
    }

    public String getRequestName() {
        return requestName;
    }

    public void setRequestName(String requestName) {
        this.requestName = requestName;
    }

    public void auth(String content) {
        headers.put("Authorization", content);
    }

    public GraphqlRequest addRetText(String text) {
        ret = text;
        return this;
    }

    @Override
    public String toString() {
        return "{" + requestName + getRequestPara().toString() + "{" + ret + "}}";
    }

    public abstract String body();
}
